<script setup>
import {computed, getCurrentInstance, ref} from "vue";
import useStore from "@/store"

const {userInfo} = useStore()

const {proxy} = getCurrentInstance()
const onClickLeft = () => proxy.$router.back()


const list = computed(() => userInfo.getAddress || [])

const defaultAddress = computed(() => userInfo.getDefaultAddress)


const chosenAddressId = ref(defaultAddress.value?.id || 0);


const onAdd = () => {
  proxy.$router.push("/addAddress")
}
const onEdit = (item, index) => {
  sessionStorage.setItem("address_detail", JSON.stringify(item))
  proxy.$router.push("/addAddress")
};

// 改变选中地址
const changeAddress = (row, index) => {
  userInfo.updateAddressIndex(index)
}
</script>

<template>
  <van-nav-bar
      title="地址列表"
      left-arrow
      @click-left="onClickLeft"
  />
  <div class="bg-[#f6f6f6] content">
    <van-address-list
        v-model="chosenAddressId"
        :list="list"
        default-tag-text="默认"
        @add="onAdd"
        @edit="onEdit"
        @select="changeAddress"
    />
  </div>

</template>

<style scoped lang="scss">
.content {
  height: calc(100vh - 46px);
  overflow: auto;
}
</style>
